package com.njtoyo.taxi.admin.logic.open;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.njtoyo.taxi.admin.mapper.master.entity.RideOrder;
import com.njtoyo.taxi.admin.mapper.master.entity.ScanRideOrder;
import com.njtoyo.taxi.admin.mapper.master.mapper.RideOrderMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.ScanRideOrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Objects;

@Service
@Slf4j
public class BigOrderNotificationLogic {
    
    @Autowired
    private RideOrderMapper rideOrderMapper;
    
    @Autowired
    private ScanRideOrderMapper scanRideOrderMapper;
    
    public String ride(Long id) {
        QueryWrapper<RideOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("ride_id", id);
        RideOrder rideOrder = rideOrderMapper.selectOne(wrapper);
        if (Objects.isNull(rideOrder)) {
            log.info("订单ID为 {} 的订单不存在", id);
            return null;
        }
        if (rideOrder.getBalancePaid().compareTo(BigDecimal.valueOf(200)) < 0) {
            log.info("余额支付部分为 {} 低于200元，无需提醒", rideOrder.getBalancePaid());
        }

        return String.format("# 大额订单支付提醒\n订单类型: 线上行程\n行程ID: %s\n订单金额: %s\n实际支付: %s\n活动补贴: %s\n余额支付: %s\n支付时间: %s", id, rideOrder.getAmount(), rideOrder.getFarePaid(), rideOrder.getAllowanceAmount(), rideOrder.getBalancePaid(), DateUtil.format(rideOrder.getLastPayTime(), DatePattern.NORM_DATETIME_PATTERN));
    }
    
    public String scanRide(Long id) {
        QueryWrapper<ScanRideOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("scan_ride_id", id);
        ScanRideOrder order = scanRideOrderMapper.selectOne(wrapper);
        if (Objects.isNull(order)) {
            log.info("订单ID为 {} 的扫码订单不存在", id);
            return null;
        }
        if (order.getAmount().compareTo(BigDecimal.valueOf(200)) < 0) {
            log.info("订单金额为 {} 低于200元，无需提醒", order.getAmount());
            return null;
        }
        
        return String.format("# 大额订单支付提醒\n行程类型: 扫码行程\n行程ID: %s\n订单金额: %s\n实际支付: %s\n支付时间: %s", id, order.getAmount(), order.getPaymentAmount(), DateUtil.format(new Date(order.getPaidTimestamp() * 1000L), DatePattern.NORM_DATETIME_PATTERN));
    }
}
